package com.lancoo.cvas.entity;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.lancoo.cvas.pojo.vo.Building;
import com.lancoo.cvas.pojo.vo.ClassTree;
import com.lancoo.cvas.pojo.vo.MajorTree;
import com.lancoo.cvas.pojo.vo.RoomVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 拆班班级
 * 
 * @author liufeng
 * @email 1037383243@qq.com
 * @date 2023-12-04 11:54:00
 */
@Data
@TableName("class_info")
@Accessors(chain = true)
@ApiModel("行政班信息")
public class ClassInfo implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * id
	 */

	@TableId(value = "id", type = IdType.AUTO)
	@ApiModelProperty("id")
	private Long id;
	/**
	 * 任务id
	 */
	@ApiModelProperty("任务id")
	private Integer taskId;
	/**
	 * 教学班id
	 */
	@ApiModelProperty("行政班id")
	private String classId;
	/**
	 * 教学班名称
	 */
	@ApiModelProperty("行政班名称")
	private String className;
	/**
	 * 年级id
	 */
	@ApiModelProperty("年级id")
	private String gradeId;
	/**
	 * 年级名称
	 */
	@ApiModelProperty("年级名称")
	private String gradeName;
	/**
	 * 专业id
	 */
	@ApiModelProperty("专业id")
	private String majorId;
	/**
	 * 专业名称
	 */
	@ApiModelProperty("专业名称")
	private String majorName;
	/**
	 * 班主任id
	 */
	@ApiModelProperty("班主任id")
	private String genderId;
	/**
	 * 班主任名称
	 */
	@ApiModelProperty("班主任名称")
	private String genderName;
	/**
	 * 教室id
	 */
	@ApiModelProperty("教室id")
	private Long roomId;
	/**
	 * 学校id
	 */
	@ApiModelProperty("学校id")
	private String schoolId;
	/**
	 * 校区id
	 */
	@ApiModelProperty("校区id")
	private String campusId;
	/**
	 * 校区名称
	 */
	@ApiModelProperty("校区名称")
	private String campusName;
	/**
	 * 学生人数
	 */
	@ApiModelProperty("学生人数")
	private Integer studentNumber;
	/**
	 * 是否拆班 0 否 1 是
	 */
	@ApiModelProperty("是否拆班")
	private Long subFlag;
	/**
	 * 拆班类型id
	 */
	@ApiModelProperty("拆班类型id")
	private Long subTypeId;

	@TableField(exist = false)
	@ApiModelProperty("排课状态")
	private Boolean scheduleState = true;

	@TableField(exist = false)
	@ApiModelProperty("课表信息")
	private List<ViewSchedule> schedules;

    public static List<ClassTree> toTree(List<ClassInfo> classInfoList) {
		List<ClassTree> gradeList = new ArrayList<>();

		for (ClassInfo classInfo : classInfoList) {
			String gradeName = classInfo.getGradeName();
			String gradeId = classInfo.getGradeId();

			ClassTree grade = null;
			for (ClassTree c : gradeList) {
				if (c.getGradeId().equals(gradeId)) {
					grade = c;
					break;
				}
			}
			if (grade == null) {
				grade = new ClassTree();
				grade.setGradeId(gradeId);
				grade.setGradeName(gradeName);
//				grade.setSum(grade.getSum() + 1);
				grade.setChildren(new ArrayList<ClassInfo>());
//				grade.setScheduleState(classInfo.getScheduleState() ? grade.getScheduleState() : false);
				gradeList.add(grade);
			}

//			Long majorId = classInfo.getMajorId();
//			String majorName = classInfo.getMajorName();
//			MajorTree majorTree = null;
//			for (MajorTree b : grade.getChildren()) {
//				if (b.getMajorId().equals(majorId)) {
//					majorTree = b;
//					break;
//				}
//			}
//			if (majorTree == null) {
//				majorTree = new MajorTree();
//				majorTree.setMajorId(majorId);
//				majorTree.setMajorName(majorName);
////				majorTree.setSum(majorTree.getSum() + 1);
//				majorTree.setChildren(new ArrayList<ClassInfo>());
////				majorTree.setScheduleState(classInfo.getScheduleState() ? majorTree.getScheduleState() : false);
//				grade.getChildren().add(majorTree);
//			}

//			majorTree.getChildren().add(classInfo);
//			majorTree.setSum(majorTree.getSum() + 1);
			grade.setSum(grade.getSum() + 1);
			grade.getChildren().add(classInfo);
//			majorTree.setScheduleState(classInfo.getScheduleState() ? majorTree.getScheduleState() : false);
			grade.setScheduleState(classInfo.getScheduleState() ? grade.getScheduleState() : false);
		}
		return gradeList.stream().peek(o -> o.getChildren().sort(Comparator.comparing(ClassInfo::getClassName)))
				.sorted(Comparator.comparing(ClassTree::getGradeName)).collect(Collectors.toList());
    }

	public int hasScheduleSize(){
		if (CollUtil.isEmpty(schedules)){
			return 0;
		}
		return 1;
	}
}
